using System;
using System.Collections.Generic;
using System.Text;

namespace Axe
{
    /// <summary>
    /// Abstract class for wise object disposing
    /// </summary>
    public abstract class DisposableBaseType : IDisposable
    {
        private bool m_Disposed = false;

        #region protected bool IsDisposed
        /// <summary>
        /// Gets a value indicating whether this instance is disposed.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this instance is disposed; otherwise, <c>false</c>.
        /// </value>
        protected bool IsDisposed
        {
            get
            {
                lock (this)
                {
                    return m_Disposed;
                }
            }
        } 
        #endregion

        #region IDisposable Members
        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        public void Dispose()
        {
            lock (this)
            {
                if (m_Disposed == false)
                {
                    Cleanup();
                    m_Disposed = true;

                    GC.SuppressFinalize(this);
                }
            }
        }
        #endregion

        /// <summary>
        /// Cleanups this instance.
        /// </summary>
        protected abstract void Cleanup();

        #region ~DisposableBaseType()
        /// <summary>
        /// Releases unmanaged resources and performs other cleanup operations before the
        /// <see cref="DisposableBaseType"/> is reclaimed by garbage collection.
        /// </summary>
        ~DisposableBaseType()
        {
            Cleanup();
        } 
        #endregion
    }
}
